//
//  ResourceURLViewController.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/5/13.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "ResourceURLViewController.h"
#import "ResourceListDataResp.h"
#import "BrowserViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "VedioPlayerViewController.h"

@interface ResourceURLViewController ()

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataSource;



@end

@implementation ResourceURLViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray new];
    [self setupView];
    // Do any additional setup after loading the view.
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (self.resourceType == 0) {
        self.title = @"产品系列介绍";
    }
    else if (self.resourceType == 1) {
        self.title = @"厂家介绍";
    }
    else if (self.resourceType == 2) {
        self.title = @"样板工程介绍";
    }
    else {
        self.title = @"实景体验";
    }
    [self loadResourceData];
}

- (void)loadResourceData {
    NSString *url;
    if (self.resourceType == 0) {
        url = HTTP_GET_PRODUCT_DOC;
    }
    else if (self.resourceType == 1) {
        url = HTTP_GET_MANUFACTURE_DOC;
    }
    else if (self.resourceType == 2) {
        url = HTTP_GET_DEMO_PROJECT;
    }
    else {
        url = HTTP_GET_DEMO_ANIMATION;
    }
    [HttpClient getWithUrl:url token:[[AccountManager manager]token] param:nil resultClass:[ResourceListDataResp class] success:^(ResourceListDataResp *obj) {
        [self.dataSource removeAllObjects];
        if (self.resourceType == 0) {
            if (obj.getProductUrlResult != nil && obj.getProductUrlResult.count > 0) {
                [self.dataSource addObjectsFromArray:obj.getProductUrlResult];
                [self.tableView reloadData];
            }
        }
        else if (self.resourceType == 1) {
            if (obj.getManufactureUrlResult != nil && obj.getManufactureUrlResult.count > 0) {
                [self.dataSource addObjectsFromArray:obj.getManufactureUrlResult];
                [self.tableView reloadData];
            }
        }
        else if (self.resourceType == 2) {
            if (obj.getSampleProjectUrlResult != nil && obj.getSampleProjectUrlResult.count > 0) {
                [self.dataSource addObjectsFromArray:obj.getSampleProjectUrlResult];
                [self.tableView reloadData];
            }
        }
        else {
            if (obj.getAnimationUrlResult != nil && obj.getAnimationUrlResult.count > 0) {
                [self.dataSource addObjectsFromArray:obj.getAnimationUrlResult];
                [self.tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        CMLog(@"获取资源失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupView {
    self.tableView = [[UITableView alloc]initWithFrame:[UIView frameWithOutNav] style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}


#pragma mark -tableView代理
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataSource.count == 0) {
        return 0;
    }
    ResourceListDataResp *data = [self.dataSource objectAtIndex:section];
    return data.resourceDatas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSource.count == 0) {
        return nil;
    }
    ResourceListDataResp *data = [self.dataSource objectAtIndex:indexPath.section];
    ResourceData *item = [data.resourceDatas objectAtIndex:indexPath.row];
    
    static NSString *identifier = @"resCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = item.title;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ResourceListDataResp *data = [self.dataSource objectAtIndex:indexPath.section];
    ResourceData *item = [data.resourceDatas objectAtIndex:indexPath.row];
    
    if ([item.url isContainsString:@"http"]) {
        BrowserViewController *view = [BrowserViewController new];
        view.title = item.title;
        view.urlStr = item.url;
        [self.navigationController pushViewController:view animated:YES];
    }
    else {
        NSArray* foo = [item.url componentsSeparatedByString: @"."];
        NSString *fileName = [NSString stringWithString:[foo objectAtIndex:0]];
        VedioPlayerViewController *view = [VedioPlayerViewController new];
        view.fileName = fileName;
        view.titleStr = item.title;
        [self.navigationController pushViewController:view animated:YES];
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 2*DEFAULT_SECTION_HEIGHT)];
    headView.backgroundColor = [UIColor whiteColor];
    
    ResourceListDataResp *data = [self.dataSource objectAtIndex:section];
    
    UILabel *label;
    label = [UILabel labelSingleLineFontSize:kActionBarFontSize content:data.group];
    label.textColor = CM_ORANGE_COLOR;
    label.x = CELL_PADDING_LEFT;
    label.centerY = headView.height/2;
    
    [headView addSubview:label];
    
    return headView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 2*DEFAULT_SECTION_HEIGHT;
}

#pragma mark -视频播放
-(void)playMovie:(NSString *)fileName{
    //视频文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];
    //视频URL
    NSURL *url = [NSURL fileURLWithPath:path];
    //视频播放对象
    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [movie prepareToPlay];
    movie.controlStyle = MPMovieControlStyleFullscreen;
    [movie.view setFrame:self.view.bounds];
    movie.initialPlaybackTime = -1;
    [self.tableView setHidden:YES];
    [self.tableView removeFromSuperview];
    [self.view addSubview:movie.view];
    // 注册一个播放结束的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(myMovieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:movie];
    [movie play];
}


/*
 @method 当视频播放完毕释放对象
 */
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
    //视频播放对象
    MPMoviePlayerController* theMovie = [notify object];
    //销毁播放通知
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:theMovie];
    [theMovie.view removeFromSuperview];
    [self.tableView setHidden:NO];
    // 释放视频对象
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
